﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace EffiProz.Core.Lib
{
    public class FileArchiver
    {
        public const int COMPRESSION_NONE = 0;
        public const int COMPRESSION_ZIP = 1;
        public const int COMPRESSION_GZIP = 2;
        private const int COPY_BLOCK_SIZE = 1 << 16;

        /*
        public static void compressFile(String infilename, String outfilename,
                                        FileAccess storage) throws IOException {
            FileArchiver.archive(infilename, outfilename, storage, COMPRESSION_ZIP);
        }

        public static void decompressFile(String infilename, String outfilename,
                                          FileAccess storage) throws IOException {
            FileArchiver.unarchive(
                    infilename, outfilename, storage, COMPRESSION_ZIP);
        }

        public static void copyFile(String infilename, String outfilename,
                                        FileAccess storage) throws IOException {
            FileArchiver.archive(
                    infilename, outfilename, storage, COMPRESSION_NONE);
        }

        public static void restoreFile(String infilename, String outfilename,
                                          FileAccess storage) throws IOException {
            FileArchiver.unarchive(
                    infilename, outfilename, storage, COMPRESSION_NONE);
        }
        */
        public static void archive(String infilename, String outfilename,
                                   EfzFileAccess storage,
                                   int compressionType)
        {

            Stream _in = null;
            Stream f = null;
            GZipStream deflater = null;
            bool completed = false;

            // if there is no file
            if (!storage.IsStreamElement(infilename))
            {
                return;
            }

            try
            {
                byte[] b = new byte[COPY_BLOCK_SIZE];

                _in = storage.OpenInputStreamElement(infilename);
                f = storage.OpenOutputStreamElement(outfilename);

                switch (compressionType)
                {

                    case COMPRESSION_ZIP:
                    case COMPRESSION_GZIP:
                        f = deflater = new GZipStream(f, CompressionMode.Compress);
                        break;

                    case COMPRESSION_NONE:
                        break;

                    default:
                        throw new Exception("FileArchiver"
                                                   + compressionType);
                }

                while (true)
                {
                    int l = _in.Read(b, 0, b.Length);

                    if (l == -1)
                    {
                        break;
                    }

                    f.Write(b, 0, l);
                }

                completed = true;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
            finally
            {
                try
                {
                    if (_in != null)
                    {
                        _in.Close();
                    }

                    if (f != null)
                    {
                        if (deflater != null)
                        {
                            deflater.Flush();
                        }

                        f.Close();
                    }

                    if (!completed && storage.IsStreamElement(outfilename))
                    {
                        storage.removeElement(outfilename);
                    }
                }
                catch (Exception e)
                {
                    throw new IOException(e.Message, e);
                }
            }
        }

        public static void unarchive(String infilename, String outfilename,
                                     EfzFileAccess storage,
                                     int compressionType)
        {

            Stream f = null;
            Stream outstream = null;
            bool completed = false;

            try
            {
                if (!storage.IsStreamElement(infilename))
                {
                    return;
                }

                storage.removeElement(outfilename);

                byte[] b = new byte[COPY_BLOCK_SIZE];

                f = storage.OpenInputStreamElement(infilename);

                switch (compressionType)
                {

                    case COMPRESSION_ZIP:
                    case COMPRESSION_GZIP:
                        f = new GZipStream(f, CompressionMode.Decompress);
                        break;

                    case COMPRESSION_NONE:
                        break;

                    default:
                        throw new Exception("FileArchiver: "
                                                   + compressionType);
                }

                outstream = storage.OpenOutputStreamElement(outfilename);

                while (true)
                {
                    int l = f.Read(b, 0, b.Length);

                    if (l == -1)
                    {
                        break;
                    }

                    outstream.Write(b, 0, l);
                }

                completed = true;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
            finally
            {
                try
                {
                    if (f != null)
                    {
                        f.Close();
                    }

                    if (outstream != null)
                    {
                        outstream.Flush();
                        outstream.Close();
                    }

                    if (!completed && storage.IsStreamElement(outfilename))
                    {
                        storage.removeElement(outfilename);
                    }
                }
                catch (Exception e)
                {
                    throw new IOException(e.Message, e);
                }
            }
        }
    }
}
